package com.jumi.microservice.controller;

import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.core.aspect.WebLog;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.domain.MessageTemplate;
import com.jumi.microservice.domain.vo.MessageTemplateVO;
import com.jumi.microservice.service.MessageTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "消息模板相关接口")
@RequestMapping("/message/MessageTemplate")
public class MessageTemplateController{

    @Resource
    private MessageTemplateService messageTemplateService;

    @ApiOperation(value = "查询数据", notes = "消息模板列表")
    @GetMapping("/list")
    public ResponseResult<List<MessageTemplateVO>> list(){
       List<MessageTemplate> messageTemplates = messageTemplateService.MessageTemplate();
       return ResultGenerator.genSuccessResult(pos2Vos(messageTemplates));
    }


    @ApiOperation(value = "启用/停用", notes = "模板消息状态启用/停用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id"),
            @ApiImplicitParam(name = "status", value = "0关，1开"),
            @ApiImplicitParam(name = "type", value = "smtMessageSwitch:站内信,smtShortSwitch:短信,smtWeixinSwitch:微信,smtAppSwitch:App推送,smtAppletsSwitch:小程序")
    })
    @PutMapping("/status")
    @WebLog(description = "更新模板消息状态启用/停用")
    public ResponseResult status(Long id, int status, String type, HttpServletRequest request){
        messageTemplateService.status(id,status,type,request);
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "编辑", notes = "模板消息编辑")
    @PutMapping("/update")
    @WebLog(description = "更新模板消息")
    public ResponseResult update(MessageTemplateVO vo,HttpServletRequest request){
        messageTemplateService.update(vo,request);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 部门DO对象转换 do->vo
     *
     * @param messageTemplates 部门do集合
     * @return 部门VO集合
     */
    private List<MessageTemplateVO> pos2Vos(List<MessageTemplate> messageTemplates) {
        List<MessageTemplateVO> pos = messageTemplates.stream().map(p -> {
            MessageTemplateVO vo = null;
            try {
                vo = p.clone(MessageTemplateVO.class);
            } catch (Exception e) {
                ExceptionEnum.FAIL_CHANGE.newException();
            }
            return vo;
        }).collect(Collectors.toList());
        return pos;
    }
}
